package com.yc.bdw.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yc.bdw.Adapter.OrderConfirmListAdapter;
import com.yc.bdw.R;
import com.yc.bdw.Utils.Constant;
import com.yc.bdw.model.Product;
import com.yc.bdw.Widget.CustomScrollView;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.BindView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class OrderConfirmActivity extends KJActivity {
    @BindView(id = R.id.check_order_back, click = true)
    private ImageView backBtn;
    @BindView(id = R.id.check_order_confirm, click = true)
    private ImageView confirmBtn;
    @BindView(id = R.id.check_order_back, click = true)
    private ListView commoditiesList;
    @BindView(id = R.id.check_order_receiver_name)
    private TextView receiverNameText;
    @BindView(id = R.id.check_order_receiver_address)
    private TextView receiverAddressText;
    @BindView(id = R.id.check_order_receiver_phone)
    private TextView receiverPhoneText;
    @BindView(id = R.id.check_order_receiver,click = true)
    private View receiverLayout;
    @BindView(id = R.id.check_order_payment_layout,click = true)
    private View paymentLayout;
    @BindView(id = R.id.check_order_payment)
    private TextView paymentText;
    @BindView(id = R.id.check_order_commodity_price)
    private TextView commodityPriceText;
    @BindView(id = R.id.check_order_delivery_price)
    private TextView deliveryPriceText;
    @BindView(id = R.id.check_order_total_price)
    private TextView totalPriceText;
    @BindView(id = R.id.check_order_scrollview)
    private CustomScrollView scrollView;

    private List<Product> products = new ArrayList<Product>();
    private OrderConfirmListAdapter adapter;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_check_order);
    }

    @Override
    public void initData() {
        super.initData();
        init();

        adapter = new OrderConfirmListAdapter(aty);
        adapter.setList(products);
    }

    private void init() {
        Product item = new Product();
        item.setPrice(BigDecimal.valueOf(11));
        item.setNumber(6);
        item.setName("酒");
        products.add(item);

        Product item2 = new Product();
        item2.setPrice(BigDecimal.valueOf(22));
        item2.setNumber(7);
        item2.setName("酒2");
        products.add(item2);

        Product item3 = new Product();
        item3.setPrice(BigDecimal.valueOf(33));
        item3.setName("酒3");
        item3.setNumber(8);
        products.add(item3);

        Product item4 = new Product();
        item4.setPrice(BigDecimal.valueOf(44));
        item4.setName("酒4");
        item4.setNumber(9);
        products.add(item4);
    }

    @Override
    public void initWidget() {
        super.initWidget();

        scrollView = (CustomScrollView) findViewById(R.id.check_order_scrollview);
        scrollView.smoothScrollTo(0, 0);

        commoditiesList = (ListView) findViewById(R.id.check_order_commodity_list);
        commoditiesList.setAdapter(adapter);
        commoditiesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                showActivity(aty, DetailActivity.class);
            }
        });
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.check_order_back:
                finish();
                break;
            case R.id.check_order_confirm:
                Bundle bundle = new Bundle();
                bundle.putString("number","121234544");
                bundle.putString("payment","微信支付");
                bundle.putString("price","892");
                showActivity(aty, OrderSubmitActivity.class, bundle);
                break;
            case R.id.check_order_receiver:
                startActivityForResult(new Intent(aty, ReceiverActivity.class), 1);
                break;
            case R.id.check_order_payment_layout:
                startActivityForResult(new Intent(aty,PaymentActivity.class),1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constant.payment_ok) {
            Bundle bundle = data.getExtras();
            String payment = bundle.getString("payment");
            paymentText.setText(payment);
        } else if ( resultCode == Constant.receiver_ok ) {
            Bundle bundle = data.getExtras();
            String address = bundle.getString("address");
            String name = bundle.getString("name");
            String phone = bundle.getString("phone");

            receiverAddressText.setText(address);
            receiverNameText.setText(name);
            receiverPhoneText.setText(phone);
        }
    }
}
